package DAO;

import Entity.ICcard;
import Entity.Manger;
import util.Get_Now_Time;
import util.Result;
import util.RfidTool;

import javax.swing.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ICcardDao {
    private static short tsector = 1;
    private static short tblk = 6;

    static Connection connect = null;//数据库连接对象
    static PreparedStatement cmmd = null;//数据库命令执行对象
    static ResultSet rs = null;//数据集
    static String sql = "";


    RfidTool instance = RfidTool.getInstance();
    String cardID = instance.showCardId();//获取id卡的号码


    public String msg() {
        String msg = instance.getdeviceInti();
        System.out.println(msg);
        return msg;
    }

    public Result insert(ICcard in) {

        Result result = new Result();
        sql = "insert into ICcard values(?,?,?,?,?,?,?)";
        try {
            Get_Now_Time getNowTime = new Get_Now_Time();
//            in.setIcDstr(getNowTime.Getnow()+2);
            in.setIcDate(getNowTime.Getnow());
            cmmd = new util.Java_to_MySql().con().prepareStatement(sql);

            cmmd.setString(1, in.getIcID());
            cmmd.setInt(2, in.getIcstatu());//传入参数的个数
            cmmd.setString(3, in.getIcDate());
            cmmd.setString(4, in.getIcDstr());
            cmmd.setString(5, in.getCardCreat());
            cmmd.setString(6, in.getIccard());
            cmmd.setString(7, in.getIDRest());

            cmmd.execute(); //执行sql语句

        } catch (Exception e1) {
            result.setStatus("-1");
            result.setMessage(e1.getMessage());
            return result;
        }
        result.setStatus("1");
        return result;
    }


    public Result update3(ICcard in) {
        Result result = new Result();
        sql = "update ICcard set Icstatus = '" + in.getIcstatu() + "',IcDate = '" + in.getIcDate() + "',IcDstr= '" + in.getIcDstr() + "',cardCreat=CONCAT(cardCreat,' " + in.getCardCreat() + "'),IcCard= '" + in.getIccard() + "', IDRest='" + in.getIDRest() + "' where IcID = '" + in.getIcID() + "'";
        try {
            cmmd = new util.Java_to_MySql().con().prepareStatement(sql);
            System.out.println(sql);
            cmmd.execute();
        } catch (Exception e1) {
            result.setStatus("-1");
            result.setMessage(e1.getMessage());
            return result;
        }
        result.setStatus("1");
        return result;

    }

    public Result Delete(String out) {
        Result result = new Result();

        sql = "delete from ICcard where IcID='"+out+"'";
        try {
            cmmd = new util.Java_to_MySql().con().prepareStatement(sql);
            System.out.println(cmmd);
            cmmd.execute(); //执行sql语句

        } catch (Exception e1) {
            result.setStatus("-1");
            result.setMessage(e1.getMessage());
            return result;
        }
        result.setStatus("1");
        return result;
    }

    public Result Querry(String x) {
        Result result = new Result();
        List<ICcard> list = new ArrayList<>();
        ICcard iCcard;
        sql = "Select * from ICcard where IcID  like '%" + x + "%' order by IcID+''";
        try {

            System.out.println(sql + "111");
            cmmd = new util.Java_to_MySql().con().prepareStatement(sql);
            //cmmd.setString(1, x);
            System.out.println(cmmd);
            rs = cmmd.executeQuery(); //把数据加载到数据集
            while (rs.next()) {
                iCcard = new ICcard();
                iCcard.setIcID(rs.getString(1));
                iCcard.setIcstatu(rs.getInt(2));
                iCcard.setIcDate(rs.getString(3));
                iCcard.setIcDstr(rs.getString(4));
                iCcard.setCardCreat(rs.getString(5));
                iCcard.setIccard(rs.getString(6));
                iCcard.setIDRest(rs.getString(7));
                list.add(iCcard);
            }
        } catch (Exception e1) {
            result.setStatus("-1");
            result.setMessage(e1.getMessage());
            return result;
        }
        result.setStatus("1");
        result.setMessage(list);
        return result;


    }


    //通过实体卡号得到卡编号
    public String getIcID(String x) throws SQLException {
        String id = null;
        Manger man = new Manger();
        sql = "SELECT IcID FROM ICcard where Iccard like '%" + x + "%'";
        try {
            cmmd = new util.Java_to_MySql().con().prepareStatement(sql);
            rs = cmmd.executeQuery();
            while (rs.next()){
                id = rs.getString(1);
            }
            return id;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return id;
    }


    public String justgetID() throws SQLException {
            String id = null;
        sql = "SELECT IcID FROM ICcard order by (IcID+0) DESC limit 1";
        try {
            cmmd = new util.Java_to_MySql().con().prepareStatement(sql);
            rs = cmmd.executeQuery();
            while (rs.next()){
               id = rs.getString(1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return id;
    }
    public ICcard get_byID(String id){
        ICcard iCcard = new ICcard();
        sql = "Select * from ICcard where IcID = '" + id + "'";
        try {
            cmmd = new util.Java_to_MySql().con().prepareStatement(sql);
            System.out.println(cmmd);
            rs = cmmd.executeQuery(); //把数据加载到数据集
            while (rs.next()) {
                iCcard.setIcID(rs.getString(1));
                iCcard.setIcstatu(rs.getInt(2));
                iCcard.setIcDate(rs.getString(3));
                iCcard.setIcDstr(rs.getString(4));
                iCcard.setCardCreat(rs.getString(5));
                iCcard.setIccard(rs.getString(6));
                iCcard.setIDRest(rs.getString(7));

            }
        } catch (Exception e1) {
            JOptionPane.showMessageDialog(null,"没有获取到信息");
        }
        return iCcard;
    }
}

